<?php
/* explain:		
 * Project:     
 * File:        qa.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Message extends Common
{
	private $intLinkSign;
	private $iUserID;
	
	private $objLog;
	
	function Message($userid)
	{
		parent::Common();
		$this->intLinkSign = $this->getConnection("system");
		
		$this->objLog 	= new Logs($userid);
		$this->iUserID 	= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getFromID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM message WHERE id = $id";
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* �������
	 *
	 * @param  string 	user id 
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $userid, $acceptid, $type, $title, $content, $level, $fromip)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO message SET userid='$userid', acceptid='$acceptid', type='$type', title='$title', content='$content', level='$level', fromip='$fromip', status='unRead', addtime=now()";
					
		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error());
		$intDataID = mysql_insert_id($this->intLinkSign);
		if($rs)
			return $intDataID;
		else
			return false;
	}

	/* modify
	 *
	 * @param  string 	userid 
 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $userid, $acceptid, $type, $title, $content, $level, $fromip)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE message SET userid='$userid', acceptid='$acceptid', type='$type', title='$title', content='$content', level='$level', fromip='$fromip', status='unRead' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* edit status
	 *
	 * @param  string 	userid 
 	 * @param  return 	boolean
	 */
	public function editStatus($arrPopedom = array(), $id)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE message SET status='read' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM message WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = 'SELECT count(id) FROM message'.$where;

		$rs  = mysql_query($sql, $this->intLinkSign);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = 'SELECT * FROM message'.$where;
		else{
			$sql = 'SELECT * FROM message'.$where." LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list by condition
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listFromCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM message".$condition;

		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}


}
?>
